const mongoose = require('mongoose')
const ObjectId = mongoose.Types.ObjectId
const { Users } = require('../../schema')

mongoose.connect('mongodb://127.0.0.1:27017/sportswear').then()

const userCollection = mongoose.model('users', Users)

function getUserList () {
    return new Promise(resolve => {
        userCollection.find({}).then(res => {
            resolve(res)
        })
    })
}

function insertUser ({ username, password }) {
    const _doc = { userid: ObjectId(), username, password, freeze: false }
    const record = new userCollection(_doc)

    return new Promise((resolve, reject) => {
        userCollection.findOne({ username }).then(res => {
            if (res) {
                reject('用户已经存在')
            } else {
                record.save().then(res => {
                    resolve('注册成功' + _doc.userid)
                }).catch(err => {
                    reject('注册失败，原因未知')
                })
            }
        })
    })
}

function checkUserLogin ({ username, password }) {
    return new Promise((resolve, reject) => {
        userCollection.findOne({ username, password }).then(res => {
            if (res) {
                resolve(res)
            } else {
                reject('用户名或密码错误')
            }
        })
    })
}

function modifyUserlist (id, data) {
    return new Promise(resolve => {
        let _data = {
            username: undefined,
            password: undefined,
            freeze: undefined
        }
        _data = Object.assign(_data, data)
        for (let key in _data) {
            if (_data[key] === undefined) {
                delete _data[key]
            }
        }
        userCollection.findOne({ username: _data.username }).then(res => {
            if (!res || res._id.toString() === id) {
                userCollection.updateOne({ _id: id }, _data).then(res => {
                    if (res.modifiedCount) {
                        resolve('修改用户成功')
                    } else {
                        resolve('用户信息保持不变')
                    }
                }).catch(err => {
                    resolve('修改用户信息失败')
                })
            } else {
                resolve('已经存在该用户名')
            }
        })
    })
}

function modifyUserPassword(userid,password){
    return new Promise(resolve => {
        userCollection.updateOne({userid},{
            password
        }).then(res=>{
            if(res.modifiedCount){
                resolve('修改密码成功')
            }else{
                resolve('密码保持不变')
            }
        })
    })
}

function deleteUserlist (id) {
    return new Promise(resolve => {
        userCollection.deleteOne({ _id: id }).then(res => {
            if (res.deletedCount) {
                resolve('注销用户成功')
            } else {
                resolve('用户已经注销')
            }
        }).catch(err => {
            resolve('用户注销失败')
        })
    })
}

module.exports = {
    insertUser,
    checkUserLogin,
    getUserList,
    modifyUserlist,
    deleteUserlist,
    modifyUserPassword
}
